<template>
  <div
    class="wrap-item"
    :style="{
      '--scroll-width': '80px',
    }"
  >
    <div class="wrap-btn-group">
      <span class="wrap-btn wrap-go-back" @click="go">返回</span>
    </div>
    <div class="wrap-other-roll">
      <!-- 基础信息 -->
      <div class="card">
        <div class="title">基础信息</div>
        <!-- type 0-第一次申请 1-企业信息变更 -->
        <el-row :gutter="12" v-if="$route.query.type && type == 1">
          <el-col :span="12">
            <item-detail :item="oldItem" />
          </el-col>
          <el-col :span="12">
            <audit-detail :item="companyBase" />
          </el-col>
        </el-row>
        <item-detail v-else :item="companyBase" />
        <div
          style="text-align: right; margin-top: 20px"
          v-if="$route.query.type"
        >
          <span class="wrap-btn wrap-success" @click="approval(1)">通过</span>
          <span class="wrap-btn wrap-danger" @click="approval(2)">驳回</span>
        </div>
      </div>
    </div>
    <!-- 审批信息 -->
    <el-dialog title="审批信息" :visible.sync="visible" width="888px">
      <el-form label-width="80px" :model="form" size="medium">
        <el-form-item label="审批意见">
          <el-input
            type="textarea"
            v-model="form.auditNote"
            maxlength="100"
            placeholder="请填写审批意见"
            rows="2"
            resize="none"
          ></el-input>
        </el-form-item>
        <el-form-item label="附件">
          <web-upload @upload="upload" type="file" />
          <el-table :data="form.auditFile" v-if="form.auditFile.length > 0">
            <el-table-column prop="name" label="名称">
              <template slot-scope="scope">
                <el-tooltip
                  popper-class="ellipsis-tip"
                  :content="scope.row.name"
                  placement="top-start"
                >
                  <span class="ellipsis">{{ scope.row.name }}</span>
                </el-tooltip>
              </template>
            </el-table-column>
            <el-table-column prop="size" label="大小"></el-table-column>
            <el-table-column
              prop="content"
              label="操作"
              width="100px"
              align="center"
            >
              <template slot-scope="scope">
                <el-link
                  type="primary"
                  :underline="false"
                  @click="downloadFile(scope.row)"
                  >下载</el-link
                >
                <el-link
                  type="danger"
                  :underline="false"
                  style="margin-left: 10px"
                  @click="remove(scope.row)"
                  >删除</el-link
                >
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
        <el-form-item style="text-align: end">
          <span class="wrap-btn wrap-success" @click="save">确定</span>
          <span class="wrap-btn wrap-default" @click="visible = false"
            >取消</span
          >
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import itemDetail from './../components/detail.vue'
import auditDetail from './auditDetail.vue'
export default {
  components: { itemDetail, auditDetail },
  data() {
    return {
      type: 0,
      companyBase: {},
      oldItem: {},
      visible: false,
      form: { auditFile: [] },
    }
  },
  created() {
    if (this.$route.query.id) {
      this.itemList()
    }
  },
  methods: {
    go() {
      //返回
      this.$store.dispatch('tags/delVisitedRoute', {
        path: this.$route.path,
      })
      this.$router.push('/supplierCenter/audit')
    },
    itemList() {
      // 获取详情
      this.$axios
        .get(
          `/admin/company/findByApprove?id=${this.$route.query.id}&runTaskId=${this.$route.query.runTaskId}`
        )
        .then((data) => {
          this.companyBase = {}
          this.oldItem = {}
          if (data.code == 200) {
            this.companyBase = data.data.companyBase || {}
            this.companyBase.userItemTypeTxt = data.data.userItemType
            this.type = data.data.type
            this.oldItem = JSON.parse(JSON.stringify(data.data.companyBase))
            if (data.data.type == 1) {
              data.data.updateItem.forEach((element) => {
                if (element.change) {
                  this.oldItem[element.name] = element.oldValue
                  this.companyBase[`${element.name}-change`] = true
                }
              })
            }
          } else {
            this.$message.error(data.msg)
          }
        })
    },
    approval(type) {
      //审批
      this.form = {
        auditFile: [],
        result: type,
        linkId: this.companyBase.linkId,
        runTaskId: this.companyBase.runTaskId,
        id: this.companyBase.id,
      }
      this.visible = true
    },
    save() {
      //保存
      if (!this.form.auditNote && this.form.result != 1) {
        this.$message.error('请填写审批意见')
        return false
      }
      this.$axios.post('/admin/company/approve', this.form).then((data) => {
        if (data.code == 200) {
          this.visible = false
          this.$message.success('操作成功！')
          this.$store.dispatch('tags/delVisitedRoute', {
            path: this.$route.path,
          })
          this.$router.push('/supplierCenter/audit')
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    upload(data) {
      //上传
      this.form.auditFile.push(data)
    },
    downloadFile(item) {
      //下载附件
      const a = document.createElement('a')
      a.download = item.name
      a.href = item.fileDomain + item.url
      a.click()
    },
    remove(item) {
      //删除
      var index = this.form.auditFile.indexOf(item)
      if (index !== -1) {
        this.form.auditFile.splice(index, 1)
      }
    },
  },
}
</script>
<style lang="scss" scoped>
.card {
  padding-bottom: 20px;
  .title {
    color: #45ab49;
    border-bottom: 1px solid #45ab49;
    padding: 10px 0;
    margin-bottom: 15px;
  }
}
</style>
